package dataGoods

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	goods_pb "mxshop/api/goods/v1"
	IGoods "mxshop/app/mxshop_api/api/internal/data/goods"
	doGoods "mxshop/app/mxshop_api/api/internal/domain/do/goods"
	"mxshop/gmicro/pkg/errors"
)

type category struct {
	gc goods_pb.GoodsClient
}

func (c *category) AllList(ctx context.Context) (*doGoods.CategoryDOList, error) {
	var ret doGoods.CategoryDOList

	rsp, err := c.gc.GetAllCategorysList(ctx, &empty.Empty{})
	if err != nil {
		return nil, errors.FromGrpcError(err)
	}

	for _, v := range rsp.Data {
		ret.Items = append(ret.Items, &doGoods.CategoryDO{
			Id:             v.Id,
			Name:           v.Name,
			ParentCategory: v.ParentCategory,
			Level:          v.Level,
			IsTab:          v.IsTab,
		})
	}
	ret.JsonData = rsp.JsonData

	return &ret, nil
}

func (c *category) List(ctx context.Context, req *goods_pb.CategoryListRequest) (*doGoods.CategoryDOList, error) {
	var ret doGoods.CategoryDOList

	rsp, err := c.gc.GetCategorysList(ctx, req)
	if err != nil {
		return nil, errors.FromGrpcError(err)
	}

	for _, v := range rsp.Data {
		ret.Items = append(ret.Items, &doGoods.CategoryDO{
			Id:             v.Id,
			Name:           v.Name,
			ParentCategory: v.ParentCategory,
			Level:          v.Level,
			IsTab:          v.IsTab,
		})
	}

	return &ret, nil
}

func (c *category) SubList(ctx context.Context, req *goods_pb.CategoryListRequest) (*doGoods.CategoryDO, error) {
	var ret doGoods.CategoryDO

	rsp, err := c.gc.GetSubCategory(ctx, req)
	if err != nil {
		return nil, errors.FromGrpcError(err)
	}

	ret = doGoods.CategoryDO{
		Id:             rsp.Info.Id,
		Name:           rsp.Info.Name,
		ParentCategory: rsp.Info.ParentCategory,
		Level:          rsp.Info.Level,
		IsTab:          rsp.Info.IsTab,
	}
	for _, v := range rsp.SubCategorys {
		ret.Subcategory = append(ret.Subcategory, &doGoods.CategoryDO{
			Id:             v.Id,
			Name:           v.Name,
			ParentCategory: v.ParentCategory,
			Level:          v.Level,
			IsTab:          v.IsTab,
		})
	}

	return &ret, nil
}

func (c *category) Create(ctx context.Context, req *doGoods.CategoryDO) (*doGoods.CategoryDO, error) {
	var ret doGoods.CategoryDO

	rsp, err := c.gc.CreateCategory(ctx, &goods_pb.CategoryInfoRequest{
		Name:           req.Name,
		ParentCategory: req.ParentCategory,
		Level:          req.Level,
		IsTab:          req.IsTab,
	})
	if err != nil {
		return nil, errors.FromGrpcError(err)
	}

	ret.Id = rsp.Id

	return &ret, nil
}

func (c *category) Update(ctx context.Context, req *doGoods.CategoryDO) error {
	_, err := c.gc.UpdateCategory(ctx, &goods_pb.CategoryInfoRequest{
		Id:             req.Id,
		Name:           req.Name,
		ParentCategory: req.ParentCategory,
		Level:          req.Level,
		IsTab:          req.IsTab,
	})
	if err != nil {
		return errors.FromGrpcError(err)
	}

	return nil
}

func (c *category) Delete(ctx context.Context, ID int64) error {
	_, err := c.gc.DeleteCategory(ctx, &goods_pb.DeleteCategoryRequest{Id: ID})
	if err != nil {
		return errors.FromGrpcError(err)
	}
	return nil
}

func newCategory(gc goods_pb.GoodsClient) IGoods.Category {
	return &category{
		gc: gc,
	}
}
